/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import db.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class UserDaoImpl implements UserDao{

    @Override
    public User login(String username, String password) {
        Connection conn = null;
	PreparedStatement pstat = null;
	ResultSet rs = null;
	String sql = "select * from user where Name=? ";
        try {
            conn = JDBCUtil.getConnection();
            pstat = conn.prepareStatement(sql);
            pstat.setString(1, username);
            rs = pstat.executeQuery();
            if(rs.next()){
                String pwd=rs.getString("Password");
                System.out.println(pwd);
                if(pwd.equals(password)){
                    JOptionPane.showMessageDialog(null, "<html><font Color='red'>登陆成功！</font>", "信息提示", 3);

                }else{
                   JOptionPane.showMessageDialog(null, "<html><font Color='red'>登陆失败！</font>", "信息提示", 3);
 
                }
            }else{
                JOptionPane.showMessageDialog(null, "<html><font Color='red'>没有此用户！</font>", "信息提示", 3);
 
            }
            
            //rs.
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
	
        return null;
    }

    @Override
    public void register(User u) {
        String sql = "insert into user(Name,Password) values(?,?)";
        try {
            Connection conn=JDBCUtil.getConnection();
            PreparedStatement pstat = conn.prepareStatement(sql);
            pstat.setString(1, u.getUsername());
            pstat.setString(2, u.getPassword());
            int count=pstat.executeUpdate();
            if(count==1){
                    System.out.println("插入成功，");
                    JOptionPane.showMessageDialog(null, "注册成功，用户名是："+u.getUsername()+" 密码是："+u.getPassword()+"！");
                }else{
                    System.out.println("插入失败。");
                    JOptionPane.showMessageDialog(null, "注册失败");
                }
        } catch (SQLException ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String findPassword(User u) {
        String sql = "select * from user where Name=?";
        try {
            Connection conn=JDBCUtil.getConnection();
            PreparedStatement pstat = conn.prepareStatement(sql);
            pstat.setString(1, u.getUsername());
            ResultSet rs=pstat.executeQuery();
            if(rs.next()){
                String userPassword=rs.getString("Password");
                u.setPassword(userPassword);
                System.out.println("用户密码是："+userPassword);
                return userPassword;
            }else{
                System.out.println("找不到对应的用户。");
                return null;
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return null;
    }

    @Override
    public boolean updatePassword(String userName, String oldPassword, String newPassword) {
        //根据用户名验证老密码是否正确，如果正确了再更新密码，并返回true,否则返回false
        String sql = "select * from user where Name=? and Password=?";
        String sql2 = "UPDATE user SET Password=? WHERE Name=?";
        try {
            Connection conn=JDBCUtil.getConnection();
            PreparedStatement pstat = conn.prepareStatement(sql);
            pstat.setString(1, userName);
            pstat.setString(2,oldPassword);
            ResultSet rs=pstat.executeQuery();
            if(rs.next()){
                
                PreparedStatement pstat2=conn.prepareStatement(sql2);
                pstat2.setString(1, newPassword);
                pstat2.setString(2,userName);
                int count=pstat2.executeUpdate();
                if(count==1){
                    System.out.println("更新密码成功。");
                    return true;
                }else{
                    System.out.println("更新密码失败。");
                    return false;
                }
            }else{
                System.out.println("找不到对应的用户，或者是旧密码错误,密码无法修改");
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
        
    }
    
}
